

import java.io.IOException;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oracle.sql.DATE;

import org.apache.tomcat.jni.Time;

/**
 * Servlet implementation class SingUp
 */
@WebServlet("/SignUp")
public class SignUp extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SignUp() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.sendRedirect("SignUp.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sentPassword = request.getParameter("password");
		String sentEmail = request.getParameter("email");
		String sentUsername = request.getParameter("username");
		
		Database baza = null;
		try {
			baza = new Database();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		baza.Select("*");
		baza.From("G_USER");
		baza.Where("EMAIL= ? ");
		PreparedStatement Vnes = null;
		try {
			Vnes = baza.SafeInput();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if(sentEmail!=""){
				Vnes.setString(1, sentEmail);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		ResultSet rs = null;
		try {
			rs = Vnes.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if(!rs.isBeforeFirst()){
				rs.close();
				baza = new Database();
				baza.InsertInto("G_USER (EMAIL, NAME, PASSWORD, STARTED_AT, USER_ID)");
				StringBuilder Values = new StringBuilder();
				Values.append("? ");
				Values.append(",");
				Values.append(" ? ");
				Values.append(",");
				Values.append(" ? ");
				Values.append(",");
				Values.append(" ? ");
				Values.append(",");
				Values.append("USER_ID.nextval");
				java.util.Calendar cal = java.util.Calendar.getInstance();
				java.util.Date utilDate = cal.getTime();
				java.sql.Date sqlDate = new Date(utilDate.getTime());
				baza.Values(Values.toString());
				Vnes = baza.SafeInput();
				Vnes.setString(1, sentEmail);
				Vnes.setString(2, sentUsername);
				Vnes.setString(3, BCrypt.hashpw(sentPassword, BCrypt.gensalt(10)));
				Vnes.setDate(4, sqlDate, cal);
				Vnes.execute();
				response.sendRedirect("Login");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
